package war2100.game;

public class GameSession {
	private Player[] player;
	private int lastIndex;
	
	public GameSession(Player ... player) {
		this.player = new Player[4];
		lastIndex = -1;
		for (Player p : player) {
			addPlayer(p);
		}
	}
	
	public int getLastIndex() {
		return lastIndex;
	}
	
	public Player getPlayer(int index) {
		return player[index-1];
	}
	
	public void addPlayer(Player p) {
		boolean added = false;
		
		for (int i=0;i<player.length;i++) {
			if (player[i] == null) {
				player[i] = p;
				if (i > lastIndex) {
					lastIndex = i;
				}
				p.setId(i+1);
				added = true;
				break;
			}
		}
		
		// player - Array zu klein, also vergrößern
		if (added == false) {
			Player[] playerCopy = new Player[player.length*2];
			for (int i=0;i<player.length;i++) {
				playerCopy[i] = player[i];
			}
			player = playerCopy;
			addPlayer(p);
		}
	}
	
	public boolean removePlayer(Player p) {
		for (int i=0;i<player.length;i++) {
			if (player[i] != null && player[i].equals(p)) {
				player[i] = null;
				player[i].setId(-1);
				return true;
			}
		}
		return false;
	}
}
